//
//  Api.m
//  zhijian
//
//  Created by yiban on 2019/1/21.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "Api.h"
#define BaseUrl @"http://cosmetic.spc.net.cn/api"
//#define BaseUrl @"http://39.105.179.69:8095/api"
@implementation ApiResponse
@end

@implementation Api
singleton_implementation(Api)
-(void)setHeader
{
    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
    NSString *versionString = [NSString stringWithFormat:@"%@",[infoDic objectForKey:@"CFBundleShortVersionString"]];
    NSMutableDictionary *dictionary =[NSMutableDictionary dictionary];
    [dictionary setValue:[NSString stringWithFormat:@"%@",[[BaseShare sharedBaseShare] getToken]] forKey:@"token"];
    [dictionary setValue:versionString forKey:@"ver"];
    /** 设置接口请求头 */
    [BaseApi setHeadr:dictionary];
}
-(void)callback:(id)responseObject error:(NSError*)error callback:(callbackBlock)callback
{
    id object = responseObject;
    if (object!=nil) {
        object =[BaseJson jsonToObject:responseObject withClass:[ApiResponse class]];
    }
    ApiResponse *res =(ApiResponse*)object;
    if (res.code==421) {
         [[NSNotificationCenter defaultCenter] postNotificationName:@"nologin" object:nil];
    }
    if (error!=nil) {
        [BaseToast show:error.localizedDescription];
        return;
    }
    if (callback) {
        callback(object,error.localizedDescription);
    }
}
- (void)login:(NSDictionary *)parameters callback:(callbackBlock)callback
{
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/user/login"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)logout:(NSDictionary *)parameters callback:(callbackBlock)callback
{
    [self setHeader];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/userInfo/loginOut"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)refresh:(NSDictionary *)parameters callback:(callbackBlock)callback
{
    [self setHeader];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/refresh"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)me:(NSDictionary *)parameters callback:(callbackBlock)callback
{
    [self setHeader];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/userInfo/getUserInfo"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
       [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)resetPwd:(NSString *)newpwd callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:newpwd forKey:@"newPassword"];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/userInfo/resetPwd"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)forgetPwd:(NSString *)cardNo
          account:(NSString *)account
      newPassword:(NSString *)newpwd
         callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:cardNo forKey:@"cardNo"];
    [parameters setValue:account forKey:@"account"];
    [parameters setValue:newpwd forKey:@"newPassword"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/user/forgetPwd"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)updateUserInfo:(NSString *)headImg
              userName:(NSString *)userName
              callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:headImg forKey:@"headImg"];
    [parameters setValue:userName forKey:@"userName"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/userInfo/updateUserInfo"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//个人
//我的收藏列表
- (void)getMyCollections:(NSString *)page
                pageSize:(NSString *)pageSize
                callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:page forKey:@"page"];
    [parameters setValue:pageSize forKey:@"pageSize"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/collection/getMyCollections"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//提交意见反馈
- (void)sendFeedBack:(NSString *)content callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:content forKey:@"content"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/feedback/sendFeedBack"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//获取我的咨询信息（仅用户）
- (void)getMyQuestionList:(NSString *)page
                 pageSize:(NSString *)pageSize
                 callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:page forKey:@"page"];
    [parameters setValue:pageSize forKey:@"pageSize"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/question/getMyQuestionList"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//获取我的回复信息
- (void)getMyReplyInfo:(NSString *)page
              pageSize:(NSString *)pageSize Callback:(callbackBlock)callback;
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:page forKey:@"page"];
    [parameters setValue:pageSize forKey:@"pageSize"];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/question/getMyReplyInfo"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//读消息
- (void)readReplyInfo:(NSString *)messageId callback:(callbackBlock)callback
{
    [self setHeader];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@?messageId=%@",BaseUrl,@"/question/readReplyInfo",messageId];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:@{} cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}

//对比搜索功能
//详细对比数据显示
- (void)getSouceDetail:(NSString *)sourceId
              callback:(callbackBlock)callback
{
    [self setHeader];
 
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@?sourceId=%@",BaseUrl,@"/source/getSouceDetail",sourceId];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:@{} cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//关键字查询-返回列表
- (void)getSouceList:(NSString *)page
            pageSize:(NSString *)pageSize
             keyType:(NSString *)keyType
                 key:(NSString *)key
          sourceType:(NSString *)sourceType
           kindsList:(NSString *)kindsList
            callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:keyType forKey:@"keyType"];
    [parameters setValue:page forKey:@"page"];
    [parameters setValue:pageSize forKey:@"pageSize"];
    [parameters setValue:key forKey:@"key"];
    [parameters setValue:sourceType forKey:@"sourceType"];
    [parameters setValue:kindsList forKey:@"kindsList"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/source/getSouceList"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//收藏
//取消收藏
- (void)collectioncancle:(NSString *)collectionId
                callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
//    [parameters setValue:collectionId forKey:@"collectionId"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@?questionId=%@",BaseUrl,@"/collection/cancle",collectionId];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//收藏问题
- (void)collectionQuestion:(NSString *)collectionId
                  callback:(callbackBlock)callback{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
//    [parameters setValue:collectionId forKey:@"collectionId"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@?questionId=%@",BaseUrl,@"/collection/collectionQuestion",collectionId];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//获取收藏信息
- (void)getCollectionInfo:(NSString *)questionId
                 callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
//    [parameters setValue:questionId forKey:@"questionId"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@?questionId=%@",BaseUrl,@"/collection/getCollectionInfo",questionId];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//咨询相关接口
//发布评论（用户和专家公用，用户评论，专家回答）
- (void)publishComment:(NSString *)questionId
        replyCommentId:(NSString *)replyCommentId
        commentContent:(NSString *)commentContent
           commentPics:(NSString *)commentPics
              callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:replyCommentId forKey:@"replyCommentId"];
    [parameters setValue:questionId forKey:@"questionId"];
    [parameters setValue:commentContent forKey:@"commentContent"];
    [parameters setValue:commentPics forKey:@"commentPics"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/question/publishComment"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//咨询详情
- (void)questionDetail:(NSString *)questionId
              callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
//    [parameters setValue:questionId forKey:@"questionId"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@?questionId=%@",BaseUrl,@"/question/questionDetail",questionId];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//评论列表
- (void)commentList:(NSString *)questionId
               page:(NSString *)page
           pageSize:(NSString *)pageSize
           callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:questionId forKey:@"questionId"];
     [parameters setValue:page forKey:@"page"];
     [parameters setValue:pageSize forKey:@"pageSize"];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/question/commentList"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
//发布咨询（仅用户）
- (void)publishQuestion:(NSString *)isOpen
        questionContent:(NSString *)questionContent
           questionPics:(NSString *)questionPics
               callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:questionContent forKey:@"questionContent"];
    [parameters setValue:questionPics forKey:@"questionPics"];
    [parameters setValue:isOpen forKey:@"isOpen"];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/question/publishQuestion"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}

//获取咨询列表（用户专家都调用这个，通过登录信息区分）
- (void)questionList:(NSString *)sortType
             content:(NSString *)content
                page:(NSString *)page
            pageSize:(NSString *)pageSize
            callback:(callbackBlock)callback
{
    [self setHeader];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [parameters setValue:sortType forKey:@"sortType"];
    [parameters setValue:content forKey:@"content"];
    [parameters setValue:page forKey:@"page"];
    [parameters setValue:pageSize forKey:@"pageSize"];
    
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/question/questionList"];
    __weak __typeof(self)weakSelf = self;
    [BaseApi POSTWithURL:Urlstr parameters:parameters cachePolicy:ApiCachePolicyIgnoreCache callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
- (void)uploadImages:(NSArray*)images callback:(callbackBlock)callback
{
    [self setHeader];
    NSString *Urlstr = [NSString stringWithFormat:@"%@%@",BaseUrl,@"/upload/files"];
    __weak __typeof(self)weakSelf = self;
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    [BaseApi uploadImageURL:Urlstr parameters:parameters images:images name:@"file" fileName:@"avatar" mimeType:@"png" progress:^(NSProgress * _Nonnull progress) {
        
    } callback:^(id  _Nonnull responseObject, BOOL isCache, NSError * _Nonnull error) {
        [weakSelf callback:responseObject error:error callback:callback];
    }];
}
@end
